<?php
require $_SERVER['DOCUMENT_ROOT'].'/../_core/common.php';

//权限验证
if( Role::init() == false ){
	exit('没有权限');
}

require $_SERVER['DOCUMENT_ROOT'].'/../_interface/category.php';

$db = new DB('news');

$created = time();
$updated = $created;
$create_uid = Sessions::get('uid');
$edit_uid = $create_uid;
$title = isset($_POST['title']) ? h($_POST['title']) : '';
$child_id = '';
$title_py = isset($_POST['title_py']) ? h($_POST['title_py']) : '';
$title_en = isset($_POST['title_en']) ? h($_POST['title_en']) : '';
$seo_keyword = isset($_POST['seo_keyword']) ? h($_POST['seo_keyword']) : '';
$seo_description = isset($_POST['seo_description']) ? h($_POST['seo_description']) : '';
$orders = isset($_POST['orders']) ? intval($_POST['orders']) : 0;
$parent_id = isset($_POST['parent_id']) ? intval($_POST['parent_id']) : 0;
$top_id = 0;
$url = isset($_POST['url']) ? h($_POST['url']) : '';
$counts = 0;
$dirs = isset($_POST['dirs']) ? h($_POST['dirs']) : '';
$ip = IP::get();
$is_del = 1;

if($parent_id < 0 || $title == '' ){
	echo '<script>alert("参数有误！");window.history.go(-1);</script>';
	exit;
}

$category_sql = 'select id from categories where title="'.$title.'" and parent_id='.$parent_id.' and is_del=1 limit 1';
$category_rs = $db->query($category_sql)->getOne();
if($category_rs['id']>0){
	echo '<script>alert("添加失败，当前栏目下《'.$title.'》已存在！");window.history.go(-1);</script>';
	exit;
}

$data=array('parent_id'=>$parent_id,
			'child_id'=>$child_id,
			'top_id'=>$top_id,
			'title'=>$title,
			'title_en'=>$title_en,
			'title_py'=>$title_py,
			'seo_keyword'=>$seo_keyword,
			'seo_description'=>$seo_description,
			'url'=>$url,
			'static_url'=>'',
			'dirs'=>$dirs,
			'create_uid'=>$create_uid,
			'edit_uid'=>$edit_uid,
			'ip'=>$ip,
			'orders'=>$orders,
			'counts'=>$counts,
			'is_del'=>$is_del,
			'created'=>$created,
			'updated'=>$updated,
			);
$news_category = $db->table('categories')->insert($data)->run();//插入
$insert_id = $db->getId();//获取刚刚插入的自增id

//根据id查找所有父级数据数组
function parent_check($db,$id){
	global $category_arr;
	$sql = 'select id,title,parent_id from categories where id='.$id.' limit 1';
	$data = $db->query($sql)->getOne();
	
	array_push($category_arr, $data);
	
	if($data['parent_id']!=0){
		parent_check($db,$data['parent_id']);
	}
}

//组装静态地址
$static_url = 'http://news.zgqw.com/h-';
$category_arr = array();
parent_check($db,$insert_id);
array_pop($category_arr);//去掉顶级的0
if(count($category_arr) == 0){//当前是一级栏目
	$top_id = 0;
	$arr_one = array('parent_id'=>$insert_id);
	$category_arr = array();
}else{
	$arr_one = array_pop($category_arr);//得到一级栏目
	$top_id = $arr_one['parent_id'];
	array_unshift($category_arr, array('parent_id'=>$insert_id));//添加当前栏目
}
$static_url .= $arr_one['parent_id'];
$category_arr = array_reverse($category_arr);
foreach($category_arr as $arr){
	$static_url .= '/'.$arr['parent_id'];
}
$static_url_data = array('static_url'=>$static_url,'top_id'=>$top_id);
$db->table('categories')->where('id='.$insert_id)->update($static_url_data)->run();//更新

$category = new Category($db);
$category -> child_add($insert_id,$insert_id);//根据id添加添加child_id

echo '<script>alert("添加成功！");window.location.href="/www/admin/news/category/category_lists.php";</script>';
exit;